package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 新增套餐和菜品关系数据
     * @param setmealDto
     * @return
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐数据
        save(setmealDto);
        //保存菜品关系表数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //补充缺少的setmealId
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐以及菜品关联数据
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐是否起售
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.in(Setmeal::getId,ids);
        slqw.eq(Setmeal::getStatus,1);
        int count = count(slqw);
        if (count>0){
            //套餐在售状态,无法删除
            throw new CustomException("套餐在售状态,无法删除");
        }
        //菜品不在售状态,可以删除
        //删除套餐数据
        removeByIds(ids);
        //删除套餐关联菜品数据
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw);
    }

    /**
     * 回显套餐和菜品关系数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getWithDish(Long id) {
        //查询套餐数据
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //数据拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询菜品关系数据
        LambdaQueryWrapper<SetmealDish> slqw = new LambdaQueryWrapper<>();
        slqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(slqw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐和菜品关系信息
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐信息
        updateById(setmealDto);
        //1删除原先的菜品关系信息
        LambdaQueryWrapper<SetmealDish> slqw = new LambdaQueryWrapper<>();
        slqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(slqw);
        //2将修改后的信息存入菜品关系表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }
}
